package com.cloud.wms.ui.data.presenter;

import com.cloud.core.base.BaseRxPresenter;
import com.cloud.core.utils.MessageCreator;
import com.cloud.wms.app.FineExApplication;
import com.cloud.wms.http.Params;
import com.cloud.wms.http.observer.LoadingObserver;
import com.cloud.wms.http.observer.ResponseWrapper;
import com.cloud.wms.tools.RxUtils;
import com.cloud.wms.ui.data.contract.CheckInventoryContact;
import com.cloud.wms.ui.data.entity.InventoryEntity;

import java.util.List;

import javax.inject.Inject;

/**
 * Created by FC on 2021/3/10.
 * Desc：库存查询
 */
public class CheckInventoryPresenter extends BaseRxPresenter<CheckInventoryContact.View> implements CheckInventoryContact.Presenter {
    public static final int CHECK_INVENTORY_SUCCESS = 0x110;

    @Inject
    public CheckInventoryPresenter() {

    }

    /**
     * 查询库存信息
     *
     * @param code
     * @param type
     */
    @Override
    public void getInventoryMsg(String code, int type) {
        Params params = Params.newInstance(Params.Type.WAREHOUSE);
        switch (type) {
            case 1:
                params.put("BarCodes", code);
                break;
            case 2:
                params.put("PosCode", code);
                break;
        }
        params.put("MaxResultCount", 100);
        FineExApplication.component().http().getInventoryMsg(params)
                .map(new ResponseWrapper<>())
                .compose(mView.toLifecycle())
                .compose(RxUtils.threadSwitch())
                .subscribe(new LoadingObserver<InventoryEntity>(mView.provideContext()) {
                    @Override
                    public void onSuccess(InventoryEntity inventoryEntity) {
                        if (inventoryEntity.getTotalCount() == 0) {
                            mView.info("未查询到符合条件的数据");
                            return;
                        }
                        mView.onSuccess(MessageCreator.createMessage(inventoryEntity, CHECK_INVENTORY_SUCCESS));
                    }

                    @Override
                    public void onError(String msg) {
                        mView.error(msg);
                    }
                });
    }
}
